#include "Input.h"
#include <Arduino.h>

Input::Input()
{
}

void Input::onInit()
{
    pinMode(ADC_PIN, INPUT);
}

void Input::onUpdate()
{
    int adcValue = analogRead(ADC_PIN);
    int pressidx = -1;
    for (int i = 0; i < 5; ++i)
    {
        if (adcValue >= BTN_THRESHOLD[i])
        {
            pressidx = i;
            break;
        }
    }

    
    for (int i = 0; i < 5; ++i)
    {

        if (pressidx == i)
        {
            if (__state[i] != PRESSED)
            {
                __state[i] = PRESSED;
                //Serial.printf("Btn %d Pressed\n", i);
            }
        }
        else if (__state[i] != RELEASED)
        {
            __state[i] = RELEASED;
            //Serial.printf("Btn %d Released\n", i);
        }
    }
    
}

ButtonState Input::getButtonState(const E_BTNS &btn)
{
    return __state[btn];
}